<? 
session_start(); 
include("backend/include/db_connect.php"); 
connect();
$sql="SET CHARACTER SET UTF8";   
query($sql); 
include "try_session_login.php";

$page = (isset($_GET['page']))? intval($_GET['page']) : 1;
$limit_end=20;
$limit_start = ($page-1)*$limit_end;
$send = "level=".$_GET['level'];
$level = $_GET['level']?$_GET['level']:1;
$sql = "SELECT 
			mbs.*,bnf.up_id,bnf.down_id,bnf.level,SUM(tb_report.insureNet) as price
		FROM 
			tb_benefi as bnf,tb_membership as mbs
		LEFT JOIN
			tb_report
		ON
			mbs.username = membershipId
		WHERE
			mbs.id = bnf.down_id
		AND
			bnf.level = '".$level."'
		AND
			bnf.up_id = '".$rs_log['id']."'
		GROUP BY
			down_id
		";
$qr = select($sql. " LIMIT " . $limit_start. "," . $limit_end);
$rows = mysql_num_rows(mysql_query($sql));
$total = count($qr);
$i=0;
?>
<script>
$(function(){
	$("a[rel='aj']").live('click', function(eve){
		$('#ui-tabs-2').html('<div align="center" style="margin:5%;"><img src="images/loading.gif"/></div>');
		var the_link = $(this).attr("href") + '&type=ajax';
		eve.preventDefault();
		$.get(the_link)
		.success(function (data){
			$('#ui-tabs-2').html(data).hide().fadeIn('slow');
		});
	
	});
	
	$("#level").change(function(e) {
        $.get('member_benefi_ajax.php',"level="+$(this).val())
		.success(function (data){
			$('#ui-tabs-2').html(data).hide().fadeIn('slow');
		});
    });
});
</script>
เลือกระดับ <select id="level">
	<option  value="1" <?php if($_GET['level']==1){echo 'selected="selected"';}?>>ระดับ 1</option>
    <option value="2" <?php if($_GET['level']==2){echo 'selected="selected"';}?>>ระดับ 2</option>
    <option value="3" <?php if($_GET['level']==3){echo 'selected="selected"';}?>>ระดับ 3</option>
    <option value="4" <?php if($_GET['level']==4){echo 'selected="selected"';}?>>ระดับ 4</option>
</select>
<table width="100%" border="1" align="center" cellpadding="3" cellspacing="0" style="border-collapse:collapse;" id="tbl_content">
  <tr class="ui-widget-header">
    <td width="64" align="center" >ลำดับ</td>
    <td width="261" align="center"  >รหัสประจำตัว</td>
    <td width="244" align="center"  >ชื่อ-นามสกุล</td>
    <td width="187" align="center"  >รายได้สุทธิ</td>
  </tr>
  <?php
	if($total > 0){ 
		while($i<count($qr)){ 
		$rs_ref=$qr[$i];
  ?>
  <tr class="ui-state-active "  onmouseout="this.className='ui-state-active'" onmouseover="this.className='ui-state-default'" id="list<?=$rs_ref['id']?>">
    <td align="center" ><?=$limit_start+$i+1;?></td>
    <td align="center"><?=$rs_ref['username']?></td>
    <td align="center"><? echo $rs_ref['name'] ?></td>
    <td align="center" valign="middle"> <?php echo number_format($rs_ref['price']);  ?></td>
  </tr>
  <?php 
      $i++;
        }//end while//
    }//end if($total > 0)//
  ?>
</table><br />
<?php 
if(($rows/$limit_end) > 1){ 

    echo "\n<div align=\"right\" class=\"pagination\">" . page_ajax($page, $limit_end, 4, $rows, $send,"mynavic","mynavi",$target,'member_benefi_ajax.php') . " </div>";

}
?>   
      